﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
namespace MyDLL
{
    //学习c#调用sqlite3的模板类
    public class Sqlite_Learn
    {
        private SQLiteConnection conn;
        private SQLiteCommand cmd;

        //连接数据库        
        public void OpenDB(string FullPath) //FullPath举例:"E:\Test.db"
        {
            conn = new SQLiteConnection("Data Source=" + FullPath);
            conn.Open();
        }

        //插入 或 修改 数据
        public void InserOrUpdate()
        {
            cmd = conn.CreateCommand();
            cmd.CommandText = "INSERT INTO user(email,name) VALUES ('email','name')";
            cmd.ExecuteNonQuery();

            cmd.CommandText = "UPDATE userSET name = 'Codelicious' WHERE ID = 1";
            cmd.ExecuteNonQuery();
        }

        //查询数据
        public void Select()
        {
            cmd.CommandText = "SELECT ID, name FROM user";
            SQLiteDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Console.WriteLine("ID: " + reader.GetInt16(0));
                    Console.WriteLine("name: " + reader.GetString(1));
                }
            }            
        }



    }
}
